package com.asit.config;

import com.asit.secure.SecureAuthenticationSuccessHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * Describe: Security 安全配置
 *
 * @author daihaoran
 * @since 2021-05-12
 */
@Configuration
@EnableWebSecurity
public class SecureConfiguration extends WebSecurityConfigurerAdapter {
    /**
     * 密码加密
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Resource
    private SecureAuthenticationSuccessHandler securityAccessSuccessHandler;
    /**
     * 身份认证接口
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("admin");
        auth.inMemoryAuthentication().withUser("mty").password("mty").roles("user");
    }

    /**
     * Describe: 配置 Security 控制逻辑
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //antMatchers("/queueServer/**").permitAll()
                // 其他的需要登录后才能访问
                .anyRequest().authenticated()
                .and()
                .formLogin().successHandler(securityAccessSuccessHandler).permitAll();
    }

}
